/**拼音库
 * author:ywx620
 * web:https://ywx620.gitee.io/learn/
*/
const PinyinFactory = function (exports) {
	function random(type=-1){
		let pys=PY_TOTAL;
		let p0=pys[0];
		let num=Math.floor(Math.random()*p0.length);
		let ps=[];
		for(let i=1;i<pys.length;i++){
			let pp=pys[i];
			for(let j=0;j<pp.length;j++){
				if(j==num){
					if(pp[j]){
						ps.push(pp[j]);
						break;
					}
				}
			}
		}
		let py=ps[Math.floor(Math.random()*ps.length)];
		//console.log(py);
		//汉语声调标注规律顺序是aoeiu，其中如果是i、u并排谁在后标谁头上，如果iú或uí
		var cags=PY_CA
		let numi=0;
		cags.find((n)=>{
			let len=n.length-1;
			let a=n[len];
			if(py.indexOf('iu')>=0){//i、u并排靠后标
				numi=py.indexOf('i');
				py=py.replace('i','');
			}
			let jqxs=['jü','qü','xü'];
			jqxs.forEach((a)=>{
				if(py.indexOf(a)>=0){
					py=py.replace('ü','u');
				}
			})
			if(py.indexOf(a)>=0){
				let num=type<=0?Math.ceil(Math.random()*4):type;
				if(num<=4){
					py=py.replace(a,n[num-1]);
				}else{
					py=py;
				}
				if(numi>0){
					py=py.split('');
					py.splice(numi,0,'i');
					py=py.join('');
				}
				return true;
			}
		})
		return py;
	}
	/***拼音转换
	 * 0：xià->xia4
	 * 1：xia4->xià
	*/
	function changePinyin(pinyin,type=0){
		//汉语声调标注规律顺序是aoeiuv，其中如果是i、u并排谁在后标谁头上，如果iú或uí
		var cags=PY_CA
		let numi=0;
		cags.find((n)=>{
			let len=n.length-1;
			if(type==0){
				for(let i=0;i<len;i++){
					let a=n[i];
					if(pinyin.indexOf(a)>=0){
						pinyin=pinyin.replace(a,n[len]);
						pinyin+=(i+1);
						return true;
					}
				}
			}else if(type==1){
				let a=n[len];
				if(pinyin.indexOf('iu')>=0){//i、u并排靠后标
					numi=pinyin.indexOf('i');
					pinyin=pinyin.replace('i','');
				}
				let jqxs=['jü','qü','xü'];
				jqxs.forEach((a)=>{
					if(pinyin.indexOf(a)>=0){
						pinyin=pinyin.replace('ü','u');
					}
				})
				if(pinyin.indexOf(a)>=0){
					let cnt=pinyin.length-1
					let num=pinyin[cnt];
					pinyin=pinyin.replace(a,n[num-1]);
					pinyin=pinyin.substr(0,cnt);
					if(numi>0){
						pinyin=pinyin.split('');
						pinyin.splice(numi,0,'i');
						pinyin=pinyin.join('');
					}
					return true;
				}
			}
		})
		return pinyin;
	}
	//检查拼音是否正确
	function check(py){
		if(py&&(typeof py === 'string')){
			for(let i=0;i<py.length;i++){
				if(!isNaN(py[i])) return false;
			}
			let pys=PY_TOTAL;
			py=changePinyin(py,0);
			py = py.replace(/[0-9]/g, '');
			//console.log(py)
			for(let i=1;i<pys.length;i++){
				let ps=pys[i];
				if(py&&ps.includes(py)) return true;
			}
		}
		return false;
	}
	//检查拼音是否有带音标
	function checkCA(py){
		let ca=PY_CA2.join('');
		if(py&&(typeof py === 'string')){
			for(let i=0;i<py.length;i++){
				if(ca.includes(py[i])) return true;
			}
		}
		return false;
	}
	/**检查拼音是否为声母韵母整体*/
	function checkCB(py,type='sm'){
		switch(type){
			case 'sm':
				return PY_SM.includes(py);
			case 'ym':
				return PY_YM.includes(py);
			case 'zt':
				return PY_ZT.includes(py);
			case'sy':
				return PY_SY.includes(py);
		}
		return false;
	}
	function all(){
		let data={sm:PY_SM,ym:PY_YM,zt:PY_ZT,ztrd:PY_ZT,sy:PY_SY,ca:PY_CA,sd:PY_SD,zw:PY_ZW,total:PY_TOTAL};
		return data;
	}
	exports.random = random;//随机获取一个拼音
	exports.changePinyin = changePinyin;//拼音的声调转换
	exports.check = check;//检查拼音是否正确
	exports.checkCA = checkCA;//检查拼音是否有带音标
	exports.checkCB = checkCB;//检查拼音是否为声母韵母整体
	exports.all = all;//获取全拼音库
    Object.defineProperty(exports, "__esModule", {
        value: true
    });
    return exports
}
({});
const PY_TOTAL=[
	["b","p","m","f","d","t","n","l","g","k","h","j","q","x","z","c","s","zh","ch","sh","r","y","w"],
	["ba","pa","ma","fa","da","ta","na","la","ga","ka","ha",,,,"za","ca","sa","zha","cha","sha",,"ya","wa"],
	["bai","pai","mai",,"dai","tai","nai","lai","gai","kai","hai",,,,"zai","cai","sai","zhai","chai","shai",,,"wai"],
	["ban","pan","man","fan","dan","tan","nan","lan","gan","kan","han",,,,"zan","can","san","zhan","chan","shan","ran","yan","wan"],
	["bang","pang","mang","fang","dang","tang","nang","lang","gang","kang","hang",,,,"zang","cang","sang","zhang","chang","shang","rang","yang","wang"],
	["bao","pao","mao",,"dao","tao","nao","lao","gao","kao","hao",,,,"zao","cao","sao","zhao","chao","shao","rao","yao",''],
	["bo","po","mo","fo",,,,,,,,,,,,,,,,,,"yo","wo"],
	[,,,,"dong","tong","nong","long","gong","kong","hong",,,,"zong","cong","song","zhong","chong",,"rong","yong",''],
	[,"pou","mou","fou","dou","tou","nou","lou","gou","kou","hou",,,,"zou","cou","sou","zhou","chou","shou","rou","you",''],
	[,,"me",,"de","te","ne","le","ge","ke","he",,,,"ze","ce","se","zhe","che","she","re","ye",''],
	["bei","pei","mei","fei","dei",,"nei","lei","gei","kei","hei",,,,"zei",,,,,"shei",,,"wei"],
	["ben","pen","men","fen","den",,"nen",,"gen","ken","hen",,,,"zen","cen","sen","zhen","chen","shen","ren",,"wen"],
	["beng","peng","meng","feng","deng","teng","neng","leng","geng","keng","heng",,,,"zeng","ceng","seng","zheng","cheng","sheng","reng",,"weng"],
	[,,,,,,,,,,,,,,,,,,,,,,''],
	["bi","pi","mi",,"di","ti","ni","li",,,,"ji","qi","xi","zi","ci","si","zhi","chi","shi","ri","yi",''],
	[,,,,"dia",,,"lia",,,,"jia","qia","xia",,,,,,,,,''],
	["biao","piao","miao",,"diao","tiao","niao","liao",,,,"jiao","qiao","xiao",,,,,,,,,''],
	["bian","pian","mian",,"dian","tian","nian","lian",,,,"jian","qian","xian",,,,,,,,,''],
	[,,,,,,"niang","liang",,,,"jiang","qiang","xiang",,,,,,,,,''],
	["bie","pie","mie",,"die","tie","nie","lie",,,,"jie","qie","xie",,,,,,,,,''],
	["bin","pin","min",,,,"nin","lin",,,,"jin","qin","xin",,,,,,,,"yin",''],
	["bing","ping","ming",,"ding","ting","ning","ling",,,,"jing","qing","xing",,,,,,,,"ying",''],
	[,,,,,,,,,,,"jiong","qiong","xiong",,,,,,,,,''],
	["bu","pu","miu",,"diu",,"niu","liu",,,,"jiu","qiu","xiu",,,,,,,,,''],
	[,,"mu","fu","du","tu","nu","lu","gu","ku","hu",,,,"zu","cu","su","zhu","chu","shu","ru","yu","wu"],
	[,,,,,,,,"gua","kua","hua",,,,,,,"zhua","chua","shua",,"yue",''],
	[,,,,,,,,"guai","kuai","huai",,,,,,,"zhuai","chuai","shuai",,,''],
	[,,,,"duan","tuan","nuan","luan","guan","kuan","huan",,,,"zuan","cuan","suan","zhuan","chuan","shuan","ruan","yuan",''],
	[,,,,,,,,"guang","kuang","huang",,,,,,,"zhuang","chuang","shuang",,,''],
	[,,,,"dui","tui",,,"gui","kui","hui",,,,"zui","cui","sui","zhui","chui","shui","rui",,''],
	[,,,,"dun","tun",,"lun","gun","kun","hun",,,,"zun","cun","sun","zhun","chun","shun","run","yun",''],
	[,,,,"duo","tuo","nuo","luo","guo","kuo","huo",,,,"zuo","cuo","suo","zhuo","chuo","shuo","ruo",,''],
	[,,,,,,"nü","lü",,,,"jü","qü","xü",,,,,,,,,''],
	[,,,,,,,,,,,"jüan","qüan","xüan",,,,,,,,,''],
	[,,,,,,"nüe","lüe",,,,"jüe","qüe","xüe",,,,,,,,,''],
	[,,,,,,,,,,,"jün","qün","xün",,,,,,,,,''],
]
const PY_SM=['b','p','m','f','d','t','n','l','g','k','h','j','q','x','z','c','s','zh','ch','sh','r','y','w']
const PY_YM=['a','o','e','i','u','ü','ai','ei','ui','ao','ou','iu','ie','üe','er','an','en','in','un','ün','ang','eng','ing','ong']
const PY_ZT=['zhi','chi','shi','ri','zi','ci','si','yi','wu','yu','ye','yue','yuan','yin','yun','ying']
const PY_SY=['ia','ua','uo','uai','iao','ian','iang','uan','uang','iong','üan']
const PY_CA=['āáǎàa','ōóǒòo','ēéěèe','īíǐìi','ūúǔùu','ǖǘǚǜü','ǖǘǚǜv'];
const PY_CA2=['āáǎà','ōóǒò','ēéěè','īíǐì','ūúǔù','ǖǘǚǜ'];
const PY_SD=['ˉ','ˊ','ˇ','ˋ'];//声调
const PY_ZW=['声母表','韵母表','整体认读音节','声调','三拼音节']